package org.airline.impl;

import java.util.List;

import org.airline.dao.IMessageDao;
import org.airline.entity.Message;
import org.airline.entity.Notice;
import org.airline.entity.PageBean;
import org.springframework.stereotype.Component;

@Component("MessageDaoImpl")
@SuppressWarnings("all")
public class MessageDaoImpl extends HibernateDaoImpl implements IMessageDao {

	// 删除留言对象
	@Override
	public void deleteMessage(int id) throws Exception {
		super.delete(Message.class, id);
	}

	// 查询所有留言对象
	@Override
	public List<Message> getAllMessage() throws Exception {
		return (List<Message>)super.find("FROM Message") ;
	}

	// 根据ID查询留言对象
	@Override
	public Message getMessageById(int id) throws Exception {
		return (Message)super.get(Message.class, id);
	}

	// 根据关键字全文搜索留言对象
	@Override
	public List<Message> getMessageByKeyWord(String keyWord) throws Exception {
		String hql = "FROM Message WHERE title LIKE '%" + keyWord + "%' OR content LIKE '%" + keyWord + "%'";
		return (List<Message>)super.find(hql) ;
	}

	// 根据标题查询留言对象
	@Override
	public List<Message> getMessageByTitle(String title) throws Exception {
		return (List<Message>)super.find("FROM Message m WHERE m.id = " + title);
	}

	// 根据用户ID查询所有留言对象
	@Override
	public List<Message> getMessageByUserId(int id) throws Exception {
		return (List<Message>)super.find("FROM Message m WHERE m.user.id = " + id);
	}

	// 持久化留言对象
	@Override
	public void saveMessage(Message message) throws Exception {
		super.save(message);
	}

	// 修改留言对象信息
	@Override
	public void updateMessage(Message message) throws Exception {
		super.update(message);
	}

	// 分页查询
	@Override
	public PageBean queryForPage(int pageSize, int page) throws Exception {
		final String hql = "FROM Message ORDER BY id DESC"; // 查询语句
		return super.queryForPage(hql, pageSize, page);
	}

	// 根据用户ID分页查询
	@Override
	public PageBean queryForPageByUserId(int id, int pageSize, int page)
			throws Exception {
		final String hql = "FROM Message WHERE user.id = " + id + " ORDER BY id DESC"; // 查询语句
		return super.queryForPage(hql, pageSize, page);
	}

	// 根据发布时间查询前5条记录
	@Override
	public List<Message> getMessageByDate() throws Exception {
		String hql = "FROM Message ORDER BY createDate DESC"; 
		return (List<Message>)super.findPageByQuery(hql, 0, 5);
	}

	//根据关键字分页查询
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize,
			int page) throws Exception {
		String hql = "FROM Message "
			+ "WHERE title LIKE '%" + keyWord + "%' "
			+ "OR content LIKE '%" + keyWord + "%' "
			+ "OR createDate LIKE '%" + keyWord + "%' "
			+ "OR user.userName like '%" + keyWord + "%' "
			+ "OR user.trueName LIKE '%" + keyWord + "%' "
			+ "OR user.sex LIKE '%" + keyWord + "%' "
			+ "OR user.tel LIKE '%" + keyWord + "%' "
			+ "OR user.email LIKE '%" + keyWord + "%' "
			+ "OR user.address LIKE '%" + keyWord + "%' ";
	return super.queryForPage(hql, pageSize, page);
	}
	
}
